package infostroy.bookagolf.components.table;

import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.components.renderer.GolfCheckBoxCellRenderer;
import infostroy.bookagolf.components.renderer.GolfHeaderRenderer;
import infostroy.bookagolf.components.renderer.GolfTableCellRenderer;
import infostroy.bookagolf.components.renderer.GolfTableCellStringRenderer;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Andrew on 17.02.14.
 */
public class GolfTable extends JTable {
    private int currentColumn;
    private Map<Integer, TableColumn> columnMap;
    private GolfHeaderRenderer headerRenderer;
    private boolean showListIcon;

    public GolfTable() {

    }

    public GolfTable(TableModel dm) {
        super(dm);
        this.customizedTable(false, 40, Colors.TABLE_HORIZONTAL_LINES_COLOR,Colors.TABLE_PANEL_BACKGROUND_COLOR, new Dimension(0,0));
    }

    public GolfTable(TableModel dm, boolean showListIcon) {
        super(dm);
        this.showListIcon = showListIcon;
        this.customizedTable(false, 40, Colors.TABLE_HORIZONTAL_LINES_COLOR,Colors.TABLE_PANEL_BACKGROUND_COLOR, new Dimension(0,0));
    }

    public GolfTable(Object[][] rowData, Object[] columnNames) {
        super(rowData, columnNames);
        this.customizedTable(false, 40, Colors.TABLE_HORIZONTAL_LINES_COLOR,Colors.TABLE_PANEL_BACKGROUND_COLOR, new Dimension(0,0));
    }

    public void customizedTable(boolean showVerticalLines, int tableRowHeight,
                                Color lineColor, Color backgroundColor,
                                Dimension interCellSpaicing) {
        this.setShowVerticalLines(showVerticalLines);
        this.setRowHeight(tableRowHeight);
        this.setGridColor(lineColor);
        this.setBackground(backgroundColor);
        this.setIntercellSpacing(interCellSpaicing);
        customizedHeader();
        fillColumnMap();
        this.setDefaultRenderer(ImageIcon.class, new GolfTableCellRenderer());
        this.setDefaultRenderer(String.class, new GolfTableCellStringRenderer());
        this.setDefaultRenderer(Boolean.class, new GolfCheckBoxCellRenderer());
    }

    private void fillColumnMap() {
        Enumeration e = this.getColumnModel().getColumns();
        columnMap = new HashMap<Integer, TableColumn>();
        int i = 0;
        while (e.hasMoreElements()) {
            TableColumn tableColumn = (TableColumn) e.nextElement();

            columnMap.put(i, tableColumn);
            i++;
        }
    }

    public void customizedColumnSize(int columnNumber, Integer minColumnWidth, Integer maxColumnWidth){

        if(minColumnWidth != null){
            columnMap.get(columnNumber).setMinWidth(minColumnWidth);
        }
        if(maxColumnWidth != null){
            columnMap.get(columnNumber).setMaxWidth(maxColumnWidth);
        }

    }

    private void customizedHeader() {
        headerRenderer =new GolfHeaderRenderer();
        headerRenderer.setShowListIcon(this.isShowListIcon());
        JTableHeader header = this.getTableHeader();
        header.setReorderingAllowed(false);
        header.setResizingAllowed(false);
        header.setDefaultRenderer(headerRenderer);
        header.setBorder(BorderFactory.createEmptyBorder());
        header.setPreferredSize(new Dimension(this.getWidth(), 40));
        header.setBackground(Colors.TABLE_HEADER_BACKGROUND_COLOR);
    }

    public boolean isShowListIcon() {
        return showListIcon;
    }

    public void setShowListIcon(boolean showListIcon) {
        this.showListIcon = showListIcon;
    }
}
